package com.itheima.contrall;

import com.itheima.bean.Course;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;

import java.io.IOException;
import java.util.Scanner;

public class StudentContrall {
    private static StudentService studentService = new StudentService();
    static Scanner sc = new Scanner(System.in);

    public void start() throws IOException {
        while (true) {
            System.out.println("请输入用户id：");
            String uid = sc.next();
            System.out.println("请输入用户密码：");
            String upassword = sc.next();
            boolean flag = studentService.istrue(uid, upassword);
            if (flag) {
                System.out.println("用户登录成功");
                break;
            } else {
                System.out.println("用户登录失败，重新输入");
            }
        }
        teacherloop:
        while (true) {
            System.out.println("--------------欢迎用户进入-----------------");
            System.out.println("请选择：1.选课  2.退课  3.查看信息  4.退出");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    choosecourse();
                    break;
                case "2":
                    recourse();
                    break;
                case "3":
                    fincourse();
                    break;
                case "4":
                    System.out.println("退出系统");
                    break teacherloop;
                default:
                    System.out.println("输入错误，重新输入");
            }
        }
    }

    public static void recourse() throws IOException {
        String id;
        while (true) {
            System.out.println("请输入删除的课程编号");
            id = sc.next();
            boolean flag = studentService.isExits(id);
            if (!flag) {
                System.out.println("您输入的课程编号错误，重新输入");
            } else {
                break;
            }
        }
        studentService.deletecourse(id);
        System.out.println("删除成功");

    }

    public static void fincourse() throws IOException {
        studentService.findcourse();
    }

    public static void choosecourse() throws IOException {
        String id;
        while (true) {
            System.out.println("请输入课程编号");
            id = sc.next();
            boolean flag = studentService.isExits(id);
            if (flag) {
                break;
            } else {
                System.out.println("编号冲突，重新输入");
            }
        }
        System.out.println("请输入课程名称");
        String name = sc.next();
        System.out.println("请输入课程类型");
        String type = sc.next();
        Course course = new Course(id, name, type);

        boolean reault = studentService.addcourse(course);
        if (reault) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }
}